<?php
/**
 * 列表文件处理
 */
namespace plugins\qbt_custom_model\service;
use think\Db;

class ListHtmlService{
    private $content;

    public function __construct($model_info)
    {
        $this->content = $this->get_path($model_info);
        $tpl_path = $this->get_tpl_path($model_info['model_controller']);
        $this->content = $this->get_text_info($model_info);
        $field_info = Db::name("model_field")->where(["model_id"=>$model_info['model_id'],"field_is_index"=>1])->order("field_sort asc")->select()->toArray();
        $this->content = $this->get_list($field_info);
        file_put_contents($tpl_path,$this->content);
    }

    /**
     * 获取模版路径并载入，返回内容
     */
    private function get_path($data){
        if($data['distinguish']==1){
            $path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS.'tpl'.DS.'category_list_view.tpl';
        }elseif($data['distinguish']==2){
            $path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS.'tpl'.DS.'list_view.tpl';
        }
        $content  = file_get_contents($path);
        return $content;
    }
    /**
     * 拼接模版路径
     */
    private function get_tpl_path($model_controller){
        $str = preg_replace("/([A-Z])/","_\\1",$model_controller);
        //去除第一个_
        $str = strtolower(ltrim($str,"_"));
        $filedir = CMF_ROOT."public/themes/admin_simpleboot3/admin/".$str."/";
        Directory($filedir);
        $fileName = $filedir."index.html";
        return $fileName;
    }
    /**
     * 获取文字信息,名称,并替换
     */
    private function get_text_info($model_info){
        //文本信息
        $desc = rtrim($model_info["model_name"],"模型");
        $this->content = str_replace(array("#DESC#","#CONTROLLERNAME#"),array($desc,$model_info['model_controller']),$this->content);
        return $this->content;
    }
    /**
     * 获取列表，并拼接
     */
    private function get_list($field_info){
        $title = '';
        $content_list = '';
        foreach ($field_info as $k=>$v){
            if($v['field_type'] == "datetime"){
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $content_list .= '
             <td>{$vo.'.$v['field_name'].'|date="Y-m-d H:i:s",###}</td>
            ';
            }elseif($v['field_type'] == "outreach"){
                $arr = explode('.',$v['field_outreach']);
               foreach ($arr as $kl=>$vl){
                   if($kl>1){
                       $arr_v = explode('=',$vl);
                       if(is_array($arr_v)){
                           $content_list .= '<td>{$vo.'.$arr[0].'_'.$arr_v[1].'}</td>';
                           $title .= '
                <th>'.$arr_v[0].'</th>
            ';
                       }
                   }
               }
            }elseif($v['field_type']=='radio' || $v['field_type']=='select'){
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $btn = preg_split('/[;\r\n]+/s', $v['field_choose']);
                $content_list .= '<td>';
                $content_list .='<switch name="vo.'.$v['field_name'].'">';
                foreach ($btn as $q){
                    $arr = explode("=",$q);
                    $content_list .= '<case value="'.$arr[0].'">'.$arr[1].'</case>';
                }
                $content_list .= '</switch>';
                $content_list .= '</td>';
            }elseif ($v['field_type']=='images'){
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $field_name=$v['field_name'];
                $content_list .= '
                 <td><a href="javascript:;" onclick="album(\'{$vo.id}\',\'图片显示\',\''.$field_name.'\')">
                            <i class="fa fa-photo fa-fw"></i>
                        </a>
                       <div id="layer-photos-demo-'.$field_name.'{$vo.id}" class="layer-photos-demo" style="display: none">
                            <?php
                            $img_url =json_decode($vo[\''.$field_name.'\'],true);
                            $PHP_SELF = $_SERVER[\'PHP_SELF\'];
                            $host_url = \'http://\' . $_SERVER[\'HTTP_HOST\'] . substr($PHP_SELF, 0, strrpos($PHP_SELF, \'/\') + 1);
                        ?>
                            <volist name="img_url" id="img">
                                <img layer-pid="" layer-src="{$host_url}upload/{$img[\'url\']}" src="__ROOT__/{$img[\'url\']}" alt="" />
                            </volist>
                        </div> 
                        
                        </td>
                ';

            }elseif ($v['field_type']=='files'){
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $field_name=$v['field_name'];
                if($v['field_file_type']=='image'){
                    $content_list .= '
                 <td><a href="javascript:;" onclick="album(\'{$vo.id}\',\'图片显示\',\''.$field_name.'\')">
                            <i class="fa fa-photo fa-fw"></i>
                        </a>
                       <div id="layer-photos-demo-'.$field_name.'{$vo.id}" class="layer-photos-demo" style="display: none">
                            <?php
                            $file_url =json_decode($vo[\''.$field_name.'\'],true);
                            $PHP_SELF = $_SERVER[\'PHP_SELF\'];
                            $host_url = \'http://\' . $_SERVER[\'HTTP_HOST\'] . substr($PHP_SELF, 0, strrpos($PHP_SELF, \'/\') + 1);
                        ?>
                            <volist name="file_url" id="img">
                                <img layer-pid="" layer-src="{$host_url}upload/{$img[\'url\']}" src="__ROOT__/{$img[\'url\']}" alt="" />
                            </volist>
                        </div> 
                        
                        </td>
                ';
                }else{
                    $content_list .= '
                 <td><?php
                            $file_url =json_decode($vo[\''.$field_name.'\'],true);
                            $PHP_SELF = $_SERVER[\'PHP_SELF\'];
                            $host_url = \'http://\' . $_SERVER[\'HTTP_HOST\'] . substr($PHP_SELF, 0, strrpos($PHP_SELF, \'/\') + 1);
                        ?>
                            <volist name="file_url" id="file">
                                <?php $file_url_info = $file[\'url\'] ?>
                                <a  href="{:cmf_get_image_preview_url($file_url_info)}" target="_blank">{$file[\'name\']}</a>
                            </volist>
                      
                        </td>
                ';
                }


            }elseif ($v['field_type']=='image'){
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $field_name=$v['field_name'];
                $content_list .= '<td><a href="javascript:parent.imagePreviewDialog(\'{:cmf_get_image_preview_url($vo.'.$field_name.')}\');">
                                <i class="fa fa-photo fa-fw"></i>
                            </a></td>';

            }elseif ($v['field_type']=='file'){

                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $field_name=$v['field_name'];

                if($v['field_file_type']=='image'){
                    $content_list .= '<td><a href="javascript:parent.imagePreviewDialog(\'{:cmf_get_image_preview_url($vo.'.$field_name.')}\');">
                                <i class="fa fa-photo fa-fw"></i>
                            </a></td>';
                }else{
                    $content_list .= '<td><a  href="{:cmf_get_image_preview_url($vo.'.$field_name.')}" target="_blank">{$vo.'.$field_name.'}</a></td>';
                }

            }else{
                $title .= '
                <th>'.$v['field_nikename'].'</th>
            ';
                $content_list .= '
                 <td>{$vo.'.$v['field_name'].'}</td>
                ';
            }
        }
        $this->content = str_replace(array("#title#","#content_list#"),array($title,$content_list),$this->content);
        return $this->content;
    }
}